package com.qk.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import com.qk.management.service.impl.CourseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/*
 *  @author SHCJ
 *  @created 2025/10/7 19:39
 *  @description
 */
@RestController
@RequestMapping("/courses")
public class CourseController {
      @Autowired
      private CourseService courseService;
      //       分页查询
      @GetMapping
      public Result page(String name, Integer target, Integer subject,  Integer page, Integer pageSize){
            IPage<Course> courseIPage =courseService.selectByPage(name,target,subject,page,pageSize);
            PageResult<Course> pageResult = new PageResult<>(courseIPage.getTotal(),courseIPage.getRecords());

            return Result.success(pageResult);
      }

//      删除课程
      @DeleteMapping("/{id}")
      public Result delete(@PathVariable Integer id){
            courseService.removeById( id);
            return Result.success();
      }

//      添加课程
      @PostMapping
      public Result add(@RequestBody Course course){
            course.setCreateTime(LocalDateTime.now());
            course.setUpdateTime(LocalDateTime.now());
            courseService.save( course);
            return Result.success();
      }

//      根据id查询
      @GetMapping("/{id}")
      public Result findById(@PathVariable Integer id){
            return Result.success(courseService.getById( id));
      }

//      修改课程
      @PutMapping
      public Result update(@RequestBody Course course){
            course.setUpdateTime(LocalDateTime.now());
            courseService.updateById( course);
            return Result.success();
      }

//      查询所有课程
      @GetMapping("/list")
      public Result selectAll(){
            return Result.success(courseService.list());
      }

//      根据学科查询课程
      @GetMapping("/subject/{subject}")
      public Result selectBySubject(@PathVariable Integer subject){
            QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("subject",subject);
            return Result.success(courseService.list(queryWrapper));
      }

}
